package www.openteam.com.adapter;

import android.content.Context;
import android.support.v7.util.SortedList;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.bean.OrderApprovalitem;
import www.openteam.com.bean.ReceivableApproval;

/**
 * Created by kylin on 2016/9/26.
 */

public class ReceivableApprovalAdapter extends RecyclerView.Adapter {


    private static final String TAG = "RApprovalAapter";

    private LayoutInflater mLayoutInflater;
    private SortedList<ReceivableApproval.DataEntity> mSortedList;
    private ArrayList<OrderApprovalitem> orderApprovalitems = new ArrayList<OrderApprovalitem>();
    StringBuilder sb = new StringBuilder();
    private ButtonInterfacet buttonInterfacet;
    private Context context;
    List<Boolean> isFlag = new ArrayList<>();
    public OnLineClickListener onLineClickListener;


    public interface OnLineClickListener{
        void onClick(ReceivableApproval.DataEntity item,int postion);
    }

    public void setOnLineClickListener(OnLineClickListener onLineClickListener) {
        this.onLineClickListener = onLineClickListener;
    }

    public ReceivableApprovalAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
//        Log.d(TAG, "DataAdapter: 1");
        mSortedList = new SortedList<>(ReceivableApproval.DataEntity.class, new SortedList.Callback<ReceivableApproval.DataEntity>() {

            /**
             * 返回一个负整数（第一个参数小于第二个）、零（相等）或正整数（第一个参数大于第二个）
             */
            @Override
            public int compare(ReceivableApproval.DataEntity o1, ReceivableApproval.DataEntity o2) {

                if (Integer.parseInt(o1.getRownumber()) < Integer.parseInt(o2.getRownumber())) {
                    return -1;
                } else if (Integer.parseInt(o1.getRownumber()) > Integer.parseInt(o2.getRownumber())) {
                    return 1;
                }

                return 0;
            }

            @Override
            public boolean areContentsTheSame(ReceivableApproval.DataEntity oldItem, ReceivableApproval.DataEntity newItem) {
                Log.d(TAG, "areContentsTheSame: " + 1);
                return oldItem.getRownumber().equals(newItem.getRownumber());
            }

            @Override
            public boolean areItemsTheSame(ReceivableApproval.DataEntity item1, ReceivableApproval.DataEntity item2) {

                return item1.getRdsno().equals(item2.getRdsno());
            }

            @Override
            public void onInserted(int position, int count) {
                notifyItemRangeInserted(position, count);
            }

            @Override
            public void onRemoved(int position, int count) {
                notifyItemRangeRemoved(position, count);
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {
                notifyItemMoved(fromPosition, toPosition);
            }

            @Override
            public void onChanged(int position, int count) {
                notifyItemRangeChanged(position, count);
            }
        });
    }

    public void addItems(ArrayList<ReceivableApproval.DataEntity> list) {
        mSortedList.beginBatchedUpdates();
        for (ReceivableApproval.DataEntity itemModel : list) {
            mSortedList.add(itemModel);
            isFlag.add(false);
        }
        mSortedList.endBatchedUpdates();
    }

    public void addContext(Context context) {
        this.context = context;
    }

    public void changeData(int change, String statue) {
        mSortedList.get(change).setStatusn(statue);
    }

    public void deleteItems(ArrayList<ReceivableApproval.DataEntity> items) {
        mSortedList.beginBatchedUpdates();
        for (ReceivableApproval.DataEntity item : items) {
            mSortedList.remove(item);
        }
        mSortedList.endBatchedUpdates();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.d(TAG, "DataAdapter: 3");
        return new mViewHolder(mLayoutInflater.inflate(R.layout.item_base, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//        Log.d(TAG, "DataAdapter:4");
        mViewHolder viewHolder = (mViewHolder) holder;
        final ReceivableApproval.DataEntity item = mSortedList.get(position);


        if (item.getStatusn().equals("已审核")) {
            viewHolder.card.setCardBackgroundColor(context.getResources().getColor(R.color.red_shallow));
        } else if (item.getStatusn().equals("未审核")) {
            viewHolder.card.setCardBackgroundColor(context.getResources().getColor(R.color.green));
        } else {
            viewHolder.card.setCardBackgroundColor(context.getResources().getColor(R.color.gray));
        }
        if (isFlag.get(position)){
            viewHolder.card.setCardBackgroundColor(context.getResources().getColor(R.color.actionbar_background));
        }
        viewHolder.textView.setText(item.getRownumber() + "");
        String custName = "";
        if (!item.getCustSName().equals(null)) {
            custName = item.getCustSName();
        } else {
            custName = item.getCustname();
        }


        BigDecimal tolMoney = new BigDecimal(item.getTolMoney());

        viewHolder.textView.setText(item.getAccntdate());
        viewHolder.tv_title.setText(
//                "编号:" + item.getRownumber() +
                        "\n" + "应收款单号:" + item.getRdsno()
                        + "\n" + "客户编号:" + item.getCustid()
                        + "\n" + "客户名称:" + custName
//                        + "\n" + "付款方式:" + item.getPayName()
//                        + "\n" + "币别:" + item.getMoneyName()
                        + "\n" + "金额:" + tolMoney.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue() + "(" + item.getMoneyName() + ")"
//                        + "\n对账日期" + item.getAccntdate() + "\n" + "状态:" + item.getStatusn()
        );

        orderApprovalitems.clear();
        sb.setLength(0);
        sb.delete(0, sb.length());
        viewHolder.lineItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onLineClickListener!=null){
                    int size = isFlag.size();
                    for (int i = 0; i < size; i++) {
                        isFlag.set(i,false);
                    }
                    isFlag.set(position,true);
                    notifyDataSetChanged();
                    onLineClickListener.onClick(item,position);
                }
            }
        });
        viewHolder.btnAuditing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(buttonInterfacet!=null) {
//                  接口实例化后的而对象，调用重写后的方法
                    buttonInterfacet.onclick(v,position);
                }

            }
        });
    }

    /**
     * 按钮点击事件需要的方法
     */
    public void buttonSetOnclick(ButtonInterfacet buttonInterfacet) {
        this.buttonInterfacet = buttonInterfacet;
    }

    /**
     * 按钮点击事件对应的接口
     */
    public interface ButtonInterfacet {
        public void onclick(View view, int position);
    }

    @Override
    public int getItemCount() {

//        Log.d(TAG, "getItemCount: " +mSortedList.size());
        return mSortedList.size();
    }

    static class mViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;
        private TextView tv_title;
        //        private Button btn_autding;
//        private CardView a_card;
        @Bind(R.id.lineItem)
        LinearLayout lineItem;
        @Bind(R.id.card)
        CardView card;
        @Bind(R.id.btn_auditing)
        Button btnAuditing ;
        public mViewHolder(View itemView) {
            super(itemView);
//            Log.d(TAG, "DataAdapter: 5");
            textView = (TextView) itemView.findViewById(R.id.tv_id);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            ButterKnife.bind(this,itemView);
//            btn_autding = (Button) itemView.findViewById(R.id.btn_auditing);
//            a_card = (CardView) itemView.findViewById(R.id.a_card);
        }
    }

}